package com.wenlong.train_ticketing_system_springboot.querymodule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenlong.train_ticketing_system_springboot.querymodule.pojo.User;
import com.wenlong.train_ticketing_system_springboot.querymodule.service.UserService;
import com.wenlong.train_ticketing_system_springboot.querymodule.mapper.UserMapper;
import com.wenlong.train_ticketing_system_springboot.querymodule.utils.MD5;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{ // ,UserDetailsService

//    @Autowired
//    private UserMapper userMapper;
//
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_name", username);
//        User user = userMapper.selectOne(queryWrapper);
//        if (user == null) throw new UsernameNotFoundException("用户名不存在");
//        List<GrantedAuthority> authorities =
//                AuthorityUtils.commaSeparatedStringToAuthorityList("manager");
//        return new org.springframework.security.core.userdetails.User(user.getUserName(),
//                new BCryptPasswordEncoder().encode(user.getPassword()), authorities);
//    }

    @Override
    public String login(User user) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",user.getUserName());
        User one = baseMapper.selectOne(queryWrapper);
        if (one!=null){
            String password = user.getPassword();
            if (one.getPassword().equals(MD5.encrypt(password))){
                System.out.println("登录成功！");
                return "success";
            }else {
                System.out.println("密码错误！");
                return "false";
            }
        }else {
            System.out.println("不存在当前用户！");
            return "false";
        }
    }

    @Override
    public String register(User user) {
        if (user==null)return "false";
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",user.getUserName());
        User one = baseMapper.selectOne(wrapper);

        if (one!=null){
            System.out.println("当前用户已经存在");
            return "false";
        }
        String password = user.getPassword();
        user.setPassword(MD5.encrypt(password));
        baseMapper.insert(user);
        return "success";
    }
}




